package com.example.listossimage.util;

import org.apache.commons.codec.binary.Base64;

import java.nio.charset.Charset;

public class Base64Util {

    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    public Base64Util() {
    }

    public static byte[] encode(byte[] bytes) {
        return Base64.encodeBase64(bytes);
    }

    public static String encode2String(byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }

    public static String encode2String(String targetString) {
        byte[] bytes = targetString.getBytes(DEFAULT_CHARSET);
        return Base64.encodeBase64String(bytes);
    }

    public static byte[] encode2Byte(String targetString) {
        byte[] bytes = targetString.getBytes(DEFAULT_CHARSET);
        return Base64.encodeBase64(bytes);
    }

    public static byte[] decode(byte[] bytes) {
        return Base64.decodeBase64(bytes);
    }

    public static String decode(String targetString) {
        return new String(Base64.decodeBase64(targetString));
    }

    public static byte[] decode2Byte(String targetString) {
        byte[] bytes = targetString.getBytes(DEFAULT_CHARSET);
        return Base64.decodeBase64(bytes);
    }

    public static String decode2String(String targetString) {
        byte[] bytes = targetString.getBytes(DEFAULT_CHARSET);
        return new String(decode(bytes));
    }
}
